<?php

/**
 * 
 * @project     : innoTweets 1.0
 * @author      : Dino (DBK)
 * 
 * @package     : Wall Model
 * @description : Database functions related to user wall post
 * 
 */
class Wall_model extends CI_Model {

  function __construct() {
    parent::__construct();
  }

  function set_wall_post($user_id = NULL, $user_id_from = NULL, $msg = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id) && !empty($user_id_from) && is_numeric($user_id_from) && !empty($msg)) {
      $query_insr = array(
          'wall_post_user_id' => $user_id,
          'wall_post_from_user_id' => $user_id_from,
          'wall_post_message' => $msg,
          'wall_post_date' => date("Y-m-d H:i:s"),
          'wall_post_status' => 1
      );
      $query_exec = $this->db->insert('wall_posts', $query_insr);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result']['wall_post_id'] = $this->db->insert_id();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function get_wall_post_by_id($wall_post_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($wall_post_id) && is_numeric($wall_post_id)) {
      $query_cond = array();
      $query_cond['wall_post_id'] = $wall_post_id;
      $query_cond['wall_post_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = wall_post_user_id');
      $query_exec = $this->db->get_where('wall_posts', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_my_wall_post($user_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['wall_post_user_id'] = $user_id;
      $query_cond['wall_post_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = wall_post_from_user_id');
      $this->db->order_by('wall_post_id', 'desc');
      $query_exec = $this->db->get_where('wall_posts', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_wall_post_by_me($user_id = NULL, $limit = 10) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['wall_post_from_user_id'] = $user_id;
      $query_cond['wall_post_status'] = 1;
      $query_cond['user_status'] = 1;
      $this->db->join('users', 'user_id = wall_post_from_user_id');
      $this->db->order_by('wall_post_id', 'desc');
      $query_exec = $this->db->get_where('wall_posts', $query_cond, $limit);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_my_wall_post_extras($user_id = NULL, $current_user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id) && !empty($current_user_id) && is_numeric($current_user_id)) {
      $query = "
        SELECT
        (SELECT COUNT(wall_post_id) FROM wall_posts WHERE wall_post_user_id = $user_id AND wall_post_status = 1) AS wall_post_total_count,
        (SELECT COUNT(wall_post_id) FROM wall_posts WHERE wall_post_user_id = $user_id AND wall_post_from_user_id = $current_user_id AND wall_post_status = 1) AS your_wall_post_total_count
        FROM tweets
        ";
      $query_exec = $this->db->query($query);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->row_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function get_unread_wall_post($user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['wall_post_user_id'] = $user_id;
      $query_cond['wall_post_read_status'] = 0;
      $query_cond['wall_post_status'] = 1;
      $query_exec = $this->db->get_where('wall_posts', $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $query_exec->result_array();
        $result_data['count'] = $query_exec->num_rows();
      }
    }
    return $result_data;
  }

  function update_read_status($user_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($user_id) && is_numeric($user_id)) {
      $query_cond = array();
      $query_cond['wall_post_user_id'] = $user_id;
      $query_upda = array();
      $query_upda['wall_post_read_status'] = '1';
      $query_exec = $this->db->update('wall_posts', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function del_wall_post_by_id($wall_post_id = NULL) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($wall_post_id) && is_numeric($wall_post_id)) {
      $query_cond = array();
      $query_cond['wall_post_id'] = $wall_post_id;
      $query_upda = array();
      $query_upda['wall_post_status'] = '0';
      $query_exec = $this->db->update('wall_posts', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

  function upd_wall_post_notify_status($wall_post_id = NULL, $notify_status = 1) {
    $result_data = array();
    $result_data['status'] = 0;
    $result_data['result'] = '';
    $result_data['count'] = 0;
    if (!empty($wall_post_id) && is_numeric($wall_post_id)) {
      $query_cond = array();
      $query_cond['wall_post_id'] = $wall_post_id;
      $query_upda = array();
      $query_upda['wall_post_notification'] = $notify_status;
      $query_exec = $this->db->update('wall_posts', $query_upda, $query_cond);
      if ($query_exec == TRUE) {
        $result_data = array();
        $result_data['status'] = 1;
        $result_data['result'] = $this->db->affected_rows();
        $result_data['count'] = 1;
      }
    }
    return $result_data;
  }

}

?>
